import 'package:flutter/material.dart';

import '../../config/public_color.dart';
import '../../foundation/decoration/shadow_decoration.dart';
import '../subscript/number_subscript.dart';

/// FileName public_tab_bar
///
/// @Author lax
/// @Date 2023/3/14
///
/// @Description

class PublicTabBar extends StatelessWidget {
  static Color? defaultSelectedColor;

  static Color? defaultUnSelectedColor;

  final TabController? controller;

  /// 自定义tab
  final List<Widget>? tabs;

  /// 默认tab数据源
  final List<String>? titles;
  final List<Widget>? selectedIcons;
  final List<Widget>? unSelectedIcons;
  final List<int>? subscripts;

  final EdgeInsets? iconMargin;
  final TextStyle? tabTextStyle;

  final double? width;
  final double? height;

  final Color? color;
  final Color? selectedColor;
  final Color? unSelectedColor;
  final Color? indicatorColor;

  final bool isScrollable;

  final EdgeInsets? padding;
  final EdgeInsets? labelPadding;

  const PublicTabBar({
    super.key,
    this.controller,
    this.tabs,
    this.titles,
    this.selectedIcons,
    this.unSelectedIcons,
    this.subscripts,
    this.iconMargin,
    this.tabTextStyle,
    this.width = double.infinity,
    this.height,
    this.color = Colors.white,
    this.selectedColor,
    this.unSelectedColor,
    this.indicatorColor,
    this.isScrollable = false,
    this.padding = EdgeInsets.zero,
    this.labelPadding = const EdgeInsets.symmetric(horizontal: 8),
  });

  /// 不可点击
  static unableTabBar({
    TabController? controller,
    bool isScrollable = false,
    List<Widget>? tabs,
  }) {
    return AbsorbPointer(
      absorbing: true,
      child: PublicTabBar(
        controller: controller,
        isScrollable: isScrollable,
        tabs: tabs,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> tabList = tabs ?? [];
    if (tabList.isEmpty) {
      final selectIndex = controller?.index ?? 0;
      final titleList = titles ?? [];

      for (int i = 0; i < titleList.length; i++) {
        final selectIcon =
        selectedIcons != null && (selectedIcons ?? []).length > i
            ? selectedIcons![i]
            : null;
        final unSelectIcon =
        unSelectedIcons != null && (unSelectedIcons ?? []).length > i
            ? unSelectedIcons![i]
            : null;
        final subNumber = subscripts != null && (subscripts ?? []).length > i
            ? subscripts![i]
            : 0;

        final tab = Tab(
          iconMargin: iconMargin ?? const EdgeInsets.only(bottom: 4),
          icon: selectIndex == i ? selectIcon : unSelectIcon,
          text: titleList[i],
          // child: Text(
          //   titleList[i],
          //   style: tabTextStyle,
          //   maxLines: 1,
          // ),
        );

        late Widget item;

        if (subNumber > 0) {
          item = Stack(
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 4),
                child: tab,
              ),
              Positioned(
                top: 2,
                right: 0,
                child: NumberSubscript(
                    color: const Color(0xBBE6001A), number: subNumber),
              ),
            ],
          );
        } else {
          item = tab;
        }
        tabList.add(item);
      }
    }
    assert(tabList.isNotEmpty);

    return Container(
      width: width,
      height: height,
      decoration: ShadowDecoration(color: color),
      alignment: Alignment.center,
      child: TabBar(
        controller: controller,
        tabs: tabList,
        padding: padding,
        labelPadding: labelPadding,
        isScrollable: isScrollable,
        labelColor:
        selectedColor ?? defaultSelectedColor ?? PublicColor.primary,
        unselectedLabelColor:
        unSelectedColor ?? defaultUnSelectedColor ?? PublicColor.lightText,
        indicatorColor: indicatorColor ??
            selectedColor ??
            defaultSelectedColor ??
            PublicColor.primary,
        indicatorSize: TabBarIndicatorSize.label,
        dividerColor: Colors.transparent,
      ),
    );
  }
}
